package algotithm.leetcode.twopoint.test978;

/**
 * @author zhouyanxiang
 * @Date 2021-02-2021/2/8-9:51
 */
public class Solution {

    public static void main(String[] args) {
        int[] arr = {9,4,2,10,7,8,8,1,9};
        Solution solution  = new Solution();
        int size = solution.maxTurbulenceSize(arr);
        System.out.println(size);
    }

    public int maxTurbulenceSize(int[] arr) {
        int l = 0, r = 0;
        int length = arr.length;
        int res = 1;
        while (r < length - 1) {
            if (l == r) {
                if (arr[l] == arr[l+1]) {
                    l++;
                }
                r++;
            } else {
                if (arr[r-1] > arr[r] && arr[r] < arr[r+1]) {
                    r++;
                } else if (arr[r] > arr[r-1] && arr[r] > arr[r+1]) {
                    r++;
                } else {
                    l = r;
                }
            }
            res = Math.max(res,r - l + 1);
        }
        return res;
    }

}
